package wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.impl.IMessageService;
import wechat.util.StreamUtil;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping("/message")
public class MessageController {

	private Logger log = LoggerFactory.getLogger(MessageController.class);

	@Autowired
	private IMessageService messageService;

	
	/**
	 * 
	 *      发送的数据形式。特别注意，数据请放在body里发送。
	 *      推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法
	 *         
	 *      数据形式：
	 *		 {                                       
	 *		   "touser": "UserID1|UserID2|UserID3",
	 *		   "toparty": " PartyID1 | PartyID2 ", 
	 *		   "totag": " TagID1 | TagID2 ",       
	 *		   "agentid": 1,                       
	 *		   "text": "test",                     
	 *		   "safe":0                            
	 *		}                                      
	 * 
	 * 
	 * 
	 * @param request
	 * @param appid
	 * @return
	 */
	@RequestMapping(value = "/sendText/{appid}", method = RequestMethod.POST)
	@ResponseBody
	public String sendText(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid) {
		
		
//      发送的数据形式。特别注意，数据请放在body里发送。
//      推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法	
//		   {
//			   "touser": "UserID1|UserID2|UserID3",
//			   "toparty": " PartyID1 | PartyID2 ",
//			   "totag": " TagID1 | TagID2 ",
//			   "agentid": 1,
//			   "text": "test",
//			   "safe":0
//			}
		
		
		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		
		InputStream input;
		try {
			input = request.getInputStream();

			String str = StreamUtil.inputStreamToString(input);
			log.info("MessageController -- sendText  str=" + str);

			JSONObject jsonParam = JSONObject.parseObject(str);
			log.info("MessageController -- sendText  jsonParam=" + jsonParam);
			
			return messageService.sendText(instanceResource, jsonParam);
		} catch (IOException e) {
			log.error("MessageController -- sendText 发送失败",e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error("MessageController -- sendText 发送失败",e);
			e.printStackTrace();
		}


		return "";
	}

	/**
	 *      发送的数据形式。特别注意，数据请放在body里发送。
	 *      推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法	
	 *		{
	 *			   "touser": "UserID1|UserID2|UserID3",
	 *			   "toparty": " PartyID1 | PartyID2 ",
	 *			   "totag": " TagID1 | TagID2 ",
	 *			   "agentid": 1,
	 *			   "articles":[
	 *			           {
	 *			               "title": "Title",
	 *			               "description": "Description",
	 *			               "url": "URL",
	 *			               "picurl": "PIC_URL"
	 *			           },
	 *			           {
	 *			               "title": "Title",
	 *			               "description": "Description",
	 *			               "url": "URL",
	 *			               "picurl": "PIC_URL"
	 *			           }    
	 *			       ]
	 *			}
	 * 
	 * 
	 * 
	 * @param request
	 * @param appid
	 * @return
	 */
	@RequestMapping(value = "/sendNews/{appid}", method = RequestMethod.POST)
	@ResponseBody
	public String sendNews(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid) {
		
		
//      发送的数据形式。特别注意，数据请放在body里发送。
//      推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法	
//		{
//			   "touser": "UserID1|UserID2|UserID3",
//			   "toparty": " PartyID1 | PartyID2 ",
//			   "totag": " TagID1 | TagID2 ",
//			   "agentid": 1,
//			   "articles":[
//			           {
//			               "title": "Title",
//			               "description": "Description",
//			               "url": "URL",
//			               "picurl": "PIC_URL"
//			           },
//			           {
//			               "title": "Title",
//			               "description": "Description",
//			               "url": "URL",
//			               "picurl": "PIC_URL"
//			           }    
//			       ]
//			}
		
		
		
		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		
		InputStream input;
		try {
			input = request.getInputStream();

			String str = StreamUtil.inputStreamToString(input);
			log.info("MessageController -- sendNews  str=" + str);

			JSONObject jsonParam = JSONObject.parseObject(str);
			log.info("MessageController -- sendNews  jsonParam=" + jsonParam);
			
			return messageService.sendNews(instanceResource, jsonParam);
		} catch (IOException e) {
			log.error("MessageController -- sendNews 发送失败",e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error("MessageController -- sendNews 发送失败",e);
			e.printStackTrace();
		}


		return "";
	}

	

}
